home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 8 / Engine / ResourceManagement.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-03-29  |  7.3 KB  |  227 lines

  1. //-----------------------------------------------------------------------------
  2. // Used to manage the creation and destruction of any resource type.
  3. // ResourceManager will ensure there is no redundancy.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #ifndef RESOURCE_MANAGEMENT_H
  9. #define RESOURCE_MANAGEMENT_H
  10.  
  11. //-----------------------------------------------------------------------------
  12. // Resource Class
  13. //-----------------------------------------------------------------------------
  14. template< class Type > class Resource
  15. {
  16. public:
  17.     //-------------------------------------------------------------------------
  18.     // The resource class constructor.
  19.     //-------------------------------------------------------------------------
  20.     Resource( char *name, char *path = "./" )
  21.     {
  22.         // Store the name.
  23.         if( name != NULL )
  24.         {
  25.             m_name = new char[strlen( name ) + 1];
  26.             memcpy( m_name, name, ( strlen( name ) + 1 ) * sizeof( char ) );
  27.         }
  28.  
  29.         // Store the path.
  30.         if( path != NULL )
  31.         {
  32.             m_path = new char[strlen( path ) + 1];
  33.             memcpy( m_path, path, ( strlen( path ) + 1 ) * sizeof( char ) );
  34.         }
  35.  
  36.         // Create the filename.
  37.         if( name != NULL && path != NULL )
  38.         {
  39.             m_filename = new char[strlen( name ) + strlen( path ) + 1];
  40.             sprintf( m_filename, "%s%s", path, name );
  41.         }
  42.  
  43.         // Start the reference count.
  44.         m_refCount = 1;
  45.     }
  46.  
  47.     //-------------------------------------------------------------------------
  48.     // The resource class destructor.
  49.     //-------------------------------------------------------------------------
  50.     virtual ~Resource()
  51.     {
  52.         SAFE_DELETE_ARRAY( m_name );
  53.         SAFE_DELETE_ARRAY( m_path );
  54.         SAFE_DELETE_ARRAY( m_filename );
  55.     }
  56.  
  57.     //-------------------------------------------------------------------------
  58.     // Returns the name of the resource.
  59.     //-------------------------------------------------------------------------
  60.     char *GetName()
  61.     {
  62.         return m_name;
  63.     }
  64.  
  65.     //-------------------------------------------------------------------------
  66.     // Returns the path to the resource.
  67.     //-------------------------------------------------------------------------
  68.     char *GetPath()
  69.     {
  70.         return m_path;
  71.     }
  72.  
  73.     //-------------------------------------------------------------------------
  74.     // Returns the filename of the resource.
  75.     //-------------------------------------------------------------------------
  76.     char *GetFilename()
  77.     {
  78.         return m_filename;
  79.     }
  80.  
  81.     //-------------------------------------------------------------------------
  82.     // Increments the resource's reference count.
  83.     //-------------------------------------------------------------------------
  84.     void IncRef()
  85.     {
  86.         m_refCount++;
  87.     }
  88.  
  89.     //-------------------------------------------------------------------------
  90.     // Decrements the resource's reference count.
  91.     //-------------------------------------------------------------------------
  92.     void DecRef()
  93.     {
  94.         m_refCount--;
  95.     }
  96.  
  97.     //-------------------------------------------------------------------------
  98.     // Returns the resource's reference count.
  99.     //-------------------------------------------------------------------------
  100.     unsigned long GetRefCount()
  101.     {
  102.         return m_refCount;
  103.     }
  104.  
  105. private:
  106.     char *m_name; // Name of the resource.
  107.     char *m_path; // Path to the resource.
  108.     char *m_filename; // Filename (name + path) of the resource.
  109.     unsigned long m_refCount; // Reference count.
  110. };
  111.  
  112. //-----------------------------------------------------------------------------
  113. // Resource Manager Class
  114. //-----------------------------------------------------------------------------
  115. template< class Type > class ResourceManager
  116. {
  117. public:
  118.     //-------------------------------------------------------------------------
  119.     // The resource manager class constructor.
  120.     //-------------------------------------------------------------------------
  121.     ResourceManager( void (*CreateResourceFunction)( Type **resource, char *name, char *path ) = NULL )
  122.     {
  123.         m_list = new LinkedList< Type >;
  124.  
  125.         CreateResource = CreateResourceFunction;
  126.     }
  127.  
  128.     //-------------------------------------------------------------------------
  129.     // The resource manager class destructor.
  130.     //-------------------------------------------------------------------------
  131.     ~ResourceManager()
  132.     {
  133.         SAFE_DELETE( m_list );
  134.     }
  135.  
  136.     //-------------------------------------------------------------------------
  137.     // Adds a new resource to the manager.
  138.     //-------------------------------------------------------------------------
  139.     Type *Add( char *name, char *path = "./" )
  140.     {
  141.         // Ensure the list, the resource name, and its path are valid.
  142.         if( m_list == NULL || name == NULL || path == NULL )
  143.             return NULL;
  144.  
  145.         // If the element already exists, then return a pointer to it.
  146.         Type *element = GetElement( name, path );
  147.         if( element != NULL )
  148.         {
  149.             element->IncRef();
  150.             return element;
  151.         }
  152.  
  153.         // Create the resource, preferably through the application specific
  154.         // function if it is available.
  155.         Type *resource = NULL;
  156.         if( CreateResource != NULL )
  157.             CreateResource( &resource, name, path );
  158.         else
  159.             resource = new Type( name, path );
  160.  
  161.         // Add the new resource to the manager and return a pointer to it.
  162.         return m_list->Add( resource );
  163.     }
  164.  
  165.     //-------------------------------------------------------------------------
  166.     // Removes the given resource from the manager.
  167.     //-------------------------------------------------------------------------
  168.     void Remove( Type **resource )
  169.     {
  170.         // Ensure the resource to be removed and the list is valid.
  171.         if( *resource == NULL || m_list == NULL )
  172.             return;
  173.  
  174.         // Decrement the resource's reference count.
  175.         (*resource)->DecRef();
  176.  
  177.         // If the resource is no long being used then destroy it.
  178.         if( (*resource)->GetRefCount() == 0 )
  179.             m_list->Remove( resource );
  180.     }
  181.  
  182.     //-------------------------------------------------------------------------
  183.     // Empties the resource list.
  184.     //-------------------------------------------------------------------------
  185.     void EmptyList()
  186.     {
  187.         if( m_list != NULL )
  188.             m_list->Empty();
  189.     }
  190.  
  191.     //-------------------------------------------------------------------------
  192.     // Returns the list of resources.
  193.     //-------------------------------------------------------------------------
  194.     LinkedList< Type > *GetList()
  195.     {
  196.         return m_list;
  197.     }
  198.  
  199.     //-------------------------------------------------------------------------
  200.     // Returns a resource by its filename.
  201.     //-------------------------------------------------------------------------
  202.     Type *GetElement( char *name, char *path = "./" )
  203.     {
  204.         // Ensure the name and path are valid, and the list is valid and not empty.
  205.         if( name == NULL || path == NULL || m_list == NULL )
  206.             return NULL;
  207.         if( m_list->GetFirst() == NULL )
  208.             return NULL;
  209.  
  210.         // Iterate the list looking for the specified resource.
  211.         m_list->Iterate( true );
  212.         while( m_list->Iterate() )
  213.             if( strcmp( m_list->GetCurrent()->GetName(), name ) == 0 )
  214.                 if( strcmp( m_list->GetCurrent()->GetPath(), path ) == 0 )
  215.                     return m_list->GetCurrent();
  216.  
  217.         // Return NULL if the resource was not found.
  218.         return NULL;
  219.     }
  220.  
  221. private:
  222.     LinkedList< Type > *m_list; // Linked list of resources.
  223.  
  224.     void (*CreateResource)( Type **resource, char *name, char *path ); // Application specific resource creation.
  225. };
  226.  
  227. #endif